#define _CRT_SECURE_NO_WARNINGS 1

class Solution 
{
public:
    int ex(int n)
    {
        int ret = 0;
        while (n > 0)
        {
            int mid = n % 10;
            ret += mid * mid;
            n /= 10;
        }
        return ret;
    }
    bool isHappy(int n)
    {
        int slow = n;
        int fast = n;
        do
        {
            slow = ex(slow);
            fast = ex(fast);
            fast = ex(fast);
        } while (slow != fast);
        return slow == 1;
    }
};